#!/bin/bash

# 农机监控系统 - API测试脚本
# 使用说明：确保后端服务在8080端口运行

BASE_URL="http://localhost:8080/api"
echo "====== 农机监控系统 - API功能测试 ======"
echo "测试服务器: $BASE_URL"
echo ""

# 颜色定义
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 测试函数
test_api() {
    local method=$1
    local endpoint=$2
    local data=$3
    local expected_status=$4
    local test_name=$5
    
    echo -n "测试: $test_name ... "
    
    if [ -n "$data" ]; then
        response=$(curl -s -w "%{http_code}" -X $method "$BASE_URL$endpoint" \
                   -H "Content-Type: application/json" \
                   -d "$data" 2>/dev/null)
    else
        response=$(curl -s -w "%{http_code}" -X $method "$BASE_URL$endpoint" \
                   -H "Content-Type: application/json" 2>/dev/null)
    fi
    
    http_code=${response: -3}
    response_body=${response%???}
    
    if [ "$http_code" -eq "$expected_status" ]; then
        echo -e "${GREEN}通过${NC} (HTTP $http_code)"
        if [ -n "$response_body" ] && [ "$response_body" != "null" ]; then
            echo "  响应: $(echo $response_body | head -c 100)..."
        fi
    else
        echo -e "${RED}失败${NC} (期望: $expected_status, 实际: $http_code)"
        if [ -n "$response_body" ]; then
            echo "  响应: $response_body"
        fi
    fi
    echo ""
}

echo -e "${YELLOW}1. 设备管理API测试${NC}"
echo "--------------------------------"

# 获取所有设备
test_api "GET" "/devices" "" 200 "获取所有设备"

# 根据状态获取设备
test_api "GET" "/devices/status/WORKING" "" 200 "获取工作中的设备"
test_api "GET" "/devices/status/IDLE" "" 200 "获取空闲设备" 
test_api "GET" "/devices/status/OFFLINE" "" 200 "获取离线设备"

# 测试无效状态
test_api "GET" "/devices/status/INVALID" "" 400 "测试无效设备状态"

# 获取单个设备
test_api "GET" "/devices/1" "" 200 "获取设备ID=1"
test_api "GET" "/devices/999" "" 404 "获取不存在的设备"

echo -e "${YELLOW}2. 路径管理API测试${NC}"
echo "--------------------------------"

# 获取所有路径
test_api "GET" "/paths" "" 200 "获取所有路径"

# 根据设备获取路径
test_api "GET" "/paths/device/1" "" 200 "获取设备1的路径"

# 根据设备和时间范围获取路径
test_api "GET" "/paths/device/1/time-range?startTime=2024-01-01T00:00:00&endTime=2024-12-31T23:59:59" "" 200 "按时间范围获取路径"

echo -e "${YELLOW}3. 地块管理API测试${NC}"
echo "--------------------------------"

# 获取所有地块
test_api "GET" "/parcels" "" 200 "获取所有地块"

# 获取单个地块
test_api "GET" "/parcels/1" "" 200 "获取地块ID=1"
test_api "GET" "/parcels/999" "" 404 "获取不存在的地块"

# 获取地块面积
test_api "GET" "/parcels/1/area" "" 200 "获取地块1的面积"

echo -e "${YELLOW}4. 创建操作测试${NC}"
echo "--------------------------------"

# 创建新设备
DEVICE_DATA='{
  "deviceName": "测试拖拉机01",
  "deviceType": "TRACTOR", 
  "status": "OFFLINE",
  "currentPosition": {
    "x": 116.4074,
    "y": 39.9042
  }
}'
test_api "POST" "/devices" "$DEVICE_DATA" 201 "创建新设备"

# 创建新路径
PATH_DATA='{
  "deviceId": 1,
  "pathName": "测试路径",
  "pathPoints": [
    {"longitude": 116.4074, "latitude": 39.9042},
    {"longitude": 116.4084, "latitude": 39.9052}
  ],
  "startTime": "2024-01-20T08:00:00",
  "endTime": "2024-01-20T12:00:00"
}'
test_api "POST" "/paths" "$PATH_DATA" 201 "创建新路径"

echo "====== 测试完成 ======"
echo ""
echo -e "${YELLOW}注意事项：${NC}"
echo "1. 确保MySQL数据库运行并包含测试数据"
echo "2. 确保Spring Boot应用在端口8080运行"
echo "3. 如果测试失败，检查应用日志"